home *** CD-ROM | disk | FTP | other *** search
/ Mac Easy 2010 May / Mac Life Ubuntu.iso / casper / filesystem.squashfs / var / lib / python-support / python2.6 / orca / speech.pyc (.txt) < prev    next >
Encoding:
Python Compiled Bytecode  |  2009-04-20  |  10.8 KB  |  351 lines

  1. # Source Generated with Decompyle++
  2. # File: in.pyc (Python 2.6)
  3.  
  4. '''Manages the default speech server for orca.  A script can use this
  5. as its speech server, or it can feel free to create one of its own.'''
  6. __id__ = '$Id: speech.py 4469 2009-01-28 13:42:35Z wwalker $'
  7. __version__ = '$Revision: 4469 $'
  8. __date__ = '$Date: 2009-01-28 08:42:35 -0500 (Wed, 28 Jan 2009) $'
  9. __copyright__ = 'Copyright (c) 2005-2008 Sun Microsystems Inc.'
  10. __license__ = 'LGPL'
  11. import logging
  12. log = logging.getLogger('speech')
  13. import re
  14. import time
  15. import chnames
  16. import debug
  17. import keynames
  18. import orca
  19. import orca_state
  20. import settings
  21. from acss import ACSS
  22. from orca_i18n import _
  23. _speechserver = None
  24. multiCaseReg1 = re.compile('([a-z]+)([A-Z])')
  25. multiCaseReg2 = re.compile('([A-Z][A-Z]+)([A-Z][a-z]+)')
  26. multiCaseReg3 = re.compile('([A-Z])([A-Z][a-z]+)')
  27.  
  28. def getSpeechServerFactories():
  29.     '''Imports all known SpeechServer factory modules.  Returns a list
  30.     of modules that implement the getSpeechServers method, which
  31.     returns a list of speechserver.SpeechServer instances.
  32.     '''
  33.     factories = []
  34.     moduleNames = settings.speechFactoryModules
  35.     for moduleName in moduleNames:
  36.         
  37.         try:
  38.             module = __import__(moduleName, globals(), locals(), [
  39.                 ''])
  40.             factories.append(module)
  41.         continue
  42.         debug.printException(debug.LEVEL_OFF)
  43.         continue
  44.  
  45.     
  46.     return factories
  47.  
  48.  
  49. def init():
  50.     global _speechserver, _speechserver
  51.     if _speechserver:
  52.         return None
  53.     moduleName = settings.speechServerFactory
  54.     if moduleName:
  55.         debug.println(debug.LEVEL_CONFIGURATION, 'Using speech server factory: %s' % moduleName)
  56.     else:
  57.         debug.println(debug.LEVEL_CONFIGURATION, 'Speech not available.')
  58.         return None
  59.     factory = _speechserver
  60.     
  61.     try:
  62.         factory = __import__(moduleName, globals(), locals(), [
  63.             ''])
  64.     except:
  65.         
  66.         try:
  67.             moduleName = moduleName.replace('orca.', '', 1)
  68.             factory = __import__(moduleName, globals(), locals(), [
  69.                 ''])
  70.         debug.printException(debug.LEVEL_SEVERE)
  71.  
  72.  
  73.     speechServerInfo = settings.speechServerInfo
  74.     if speechServerInfo:
  75.         _speechserver = factory.SpeechServer.getSpeechServer(speechServerInfo)
  76.     else:
  77.         _speechserver = factory.SpeechServer.getSpeechServer()
  78.  
  79.  
  80. def __resolveACSS(acss = None):
  81.     if acss:
  82.         return acss
  83.     voices = settings.voices
  84.     return voices[settings.DEFAULT_VOICE]
  85.  
  86.  
  87. def sayAll(utteranceIterator, progressCallback):
  88.     if settings.silenceSpeech:
  89.         return None
  90.     if _speechserver:
  91.         _speechserver.sayAll(utteranceIterator, progressCallback)
  92.     else:
  93.         for context, acss in utteranceIterator:
  94.             logLine = "SPEECH OUTPUT: '" + context.utterance + "'"
  95.             debug.println(debug.LEVEL_INFO, logLine)
  96.             log.info(logLine)
  97.         
  98.  
  99.  
  100. def speak(text, acss = None, interrupt = True):
  101.     '''Speaks all queued text immediately.  If text is not None,
  102.     it is added to the queue before speaking.
  103.  
  104.     Arguments:
  105.     - text:      optional text to add to the queue before speaking
  106.     - acss:      acss.ACSS instance; if None,
  107.                  the default voice settings will be used.
  108.                  Otherwise, the acss settings will be
  109.                  used to augment/override the default
  110.                  voice settings.
  111.     - interrupt: if True, stops any speech in progress before
  112.                  speaking the text
  113.     '''
  114.     if orca_state.lastKeyEchoTime:
  115.         if interrupt:
  116.             pass
  117.         interrupt = time.time() - orca_state.lastKeyEchoTime > 0.5
  118.     
  119.     if settings.silenceSpeech:
  120.         return None
  121.     if settings.speakMultiCaseStringsAsWords:
  122.         text = _processMultiCaseString(text)
  123.     
  124.     if orca_state.activeScript and orca_state.usePronunciationDictionary:
  125.         text = orca_state.activeScript.adjustForPronunciation(text)
  126.     
  127.     if settings.speakMultiCaseStringsAsWords:
  128.         text = _processMultiCaseString(text)
  129.     
  130.     logLine = "SPEECH OUTPUT: '" + text + "'"
  131.     debug.println(debug.LEVEL_INFO, logLine)
  132.     log.info(logLine)
  133.     if _speechserver:
  134.         _speechserver.speak(text, __resolveACSS(acss), interrupt)
  135.     
  136.  
  137.  
  138. def speakKeyEvent(event_string, eventType):
  139.     '''Speaks a key event immediately.
  140.  
  141.     Arguments:
  142.     - event_string: string representing the key event as defined by
  143.                     input_event.KeyboardEvent.
  144.     - eventType:    key event type as one of orca.KeyEventType constants.
  145.  
  146.     '''
  147.     if settings.silenceSpeech:
  148.         return None
  149.     if _speechserver:
  150.         _speechserver.speakKeyEvent(event_string, eventType)
  151.     else:
  152.         event_string = keynames.getKeyName(event_string)
  153.         if eventType == orca.KeyEventType.LOCKING_LOCKED:
  154.             event_string += ' ' + _('on')
  155.         elif eventType == orca.KeyEventType.LOCKING_UNLOCKED:
  156.             event_string += ' ' + _('off')
  157.         
  158.         logLine = "SPEECH OUTPUT: '" + event_string + "'"
  159.         debug.println(debug.LEVEL_INFO, logLine)
  160.         log.info(logLine)
  161.  
  162.  
  163. def speakCharacter(character, acss = None):
  164.     '''Speaks a single character immediately.
  165.  
  166.     Arguments:
  167.     - character: text to be spoken
  168.     - acss:      acss.ACSS instance; if None,
  169.                  the default voice settings will be used.
  170.                  Otherwise, the acss settings will be
  171.                  used to augment/override the default
  172.                  voice settings.
  173.     '''
  174.     if settings.silenceSpeech:
  175.         return None
  176.     spokenCharacter = chnames.getCharacterName(character)
  177.     debug.println(debug.LEVEL_INFO, "SPEECH OUTPUT: '" + spokenCharacter + "'")
  178.     log.info("SPEECH OUTPUT: '%s'" % spokenCharacter)
  179.     if _speechserver:
  180.         _speechserver.speakCharacter(character, acss = acss)
  181.     
  182.  
  183.  
  184. def isSpeaking():
  185.     '''Returns True if the system is currently speaking.'''
  186.     if _speechserver:
  187.         return _speechserver.isSpeaking()
  188.     return False
  189.  
  190.  
  191. def speakUtterances(utterances, acss = None, interrupt = True):
  192.     '''Speaks the given list of utterances immediately.
  193.  
  194.     Arguments:
  195.     - list:      list of strings to be spoken
  196.     - acss:      acss.ACSS instance; if None,
  197.                  the default voice settings will be used.
  198.                  Otherwise, the acss settings will be
  199.                  used to augment/override the default
  200.                  voice settings.
  201.     - interrupt: if True, stop any speech currently in progress.
  202.     '''
  203.     if orca_state.lastKeyEchoTime:
  204.         if interrupt:
  205.             pass
  206.         interrupt = time.time() - orca_state.lastKeyEchoTime > 0.5
  207.     
  208.     if settings.silenceSpeech:
  209.         return None
  210.     i = 0
  211.     length = len(utterances)
  212.     while i < length:
  213.         if settings.speakMultiCaseStringsAsWords:
  214.             utterances[i] = _processMultiCaseString(utterances[i])
  215.         
  216.         if orca_state.activeScript and orca_state.usePronunciationDictionary:
  217.             utterances[i] = orca_state.activeScript.adjustForPronunciation(utterances[i])
  218.         
  219.         if settings.speakMultiCaseStringsAsWords:
  220.             utterances[i] = _processMultiCaseString(utterances[i])
  221.         
  222.         logLine = "SPEECH OUTPUT: '" + utterances[i] + "'"
  223.         debug.println(debug.LEVEL_INFO, logLine)
  224.         log.info(logLine)
  225.         i = i + 1
  226.     if _speechserver:
  227.         _speechserver.speakUtterances(utterances, __resolveACSS(acss), interrupt)
  228.     
  229.  
  230.  
  231. def getInfo():
  232.     info = None
  233.     if _speechserver:
  234.         info = _speechserver.getInfo()
  235.     
  236.     return info
  237.  
  238.  
  239. def stop():
  240.     if _speechserver:
  241.         _speechserver.stop()
  242.     
  243.  
  244.  
  245. def increaseSpeechRate(script = None, inputEvent = None):
  246.     if _speechserver:
  247.         _speechserver.increaseSpeechRate()
  248.     else:
  249.         logLine = "SPEECH OUTPUT: 'faster'"
  250.         debug.println(debug.LEVEL_INFO, logLine)
  251.         log.info(logLine)
  252.     return True
  253.  
  254.  
  255. def decreaseSpeechRate(script = None, inputEvent = None):
  256.     if _speechserver:
  257.         _speechserver.decreaseSpeechRate()
  258.     else:
  259.         logLine = "SPEECH OUTPUT: 'slower'"
  260.         debug.println(debug.LEVEL_INFO, logLine)
  261.         log.info(logLine)
  262.     return True
  263.  
  264.  
  265. def increaseSpeechPitch(script = None, inputEvent = None):
  266.     if _speechserver:
  267.         _speechserver.increaseSpeechPitch()
  268.     else:
  269.         logLine = "SPEECH OUTPUT: 'higher'"
  270.         debug.println(debug.LEVEL_INFO, logLine)
  271.         log.info(logLine)
  272.     return True
  273.  
  274.  
  275. def decreaseSpeechPitch(script = None, inputEvent = None):
  276.     if _speechserver:
  277.         _speechserver.decreaseSpeechPitch()
  278.     else:
  279.         logLine = "SPEECH OUTPUT: 'lower'"
  280.         debug.println(debug.LEVEL_INFO, logLine)
  281.         log.info(logLine)
  282.     return True
  283.  
  284.  
  285. def shutdown():
  286.     global _speechserver
  287.     if _speechserver:
  288.         _speechserver.shutdownActiveServers()
  289.         _speechserver = None
  290.     
  291.  
  292.  
  293. def reset(text = None, acss = None):
  294.     if _speechserver:
  295.         _speechserver.reset(text, acss)
  296.     
  297.  
  298.  
  299. def testNoSettingsInit():
  300.     init()
  301.     speak('testing')
  302.     speak('this is higher', ACSS({
  303.         'average-pitch': 7 }))
  304.     speak('this is slower', ACSS({
  305.         'rate': 3 }))
  306.     speak('this is faster', ACSS({
  307.         'rate': 80 }))
  308.     speak('this is quiet', ACSS({
  309.         'gain': 2 }))
  310.     speak('this is loud', ACSS({
  311.         'gain': 10 }))
  312.     speak('this is normal')
  313.  
  314.  
  315. def test():
  316.     import speechserver
  317.     factories = getSpeechServerFactories()
  318.     for factory in factories:
  319.         print factory.__name__
  320.         servers = factory.SpeechServer.getSpeechServers()
  321.         for server in servers:
  322.             
  323.             try:
  324.                 print '    ', server.getInfo()
  325.                 for family in server.getVoiceFamilies():
  326.                     name = family[speechserver.VoiceFamily.NAME]
  327.                     print '      ', name
  328.                     acss = ACSS({
  329.                         ACSS.FAMILY: family })
  330.                     server.speak(name, acss)
  331.                     server.speak('testing')
  332.                 
  333.                 server.shutdown()
  334.             continue
  335.             debug.printException(debug.LEVEL_OFF)
  336.             continue
  337.  
  338.         
  339.     
  340.  
  341.  
  342. def _processMultiCaseString(string):
  343.     '''Helper function, applies the regexes to split multiCaseStrings
  344.     to multiple words.
  345.     '''
  346.     string = multiCaseReg1.sub('\\1 \\2', string)
  347.     string = multiCaseReg2.sub('\\1 \\2', string)
  348.     string = multiCaseReg3.sub('\\1 \\2', string)
  349.     return string
  350.  
  351.